﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Mvc;
using System.IO;
using Castle.Core.Internal;
using System.Text;
using Microsoft.AspNetCore.Hosting;
using Coldairarrow.Business;
using System.Threading.Tasks;
using Coldairarrow.Util;
using SelectPdf;
using Coldairarrow.Entity.TurnPDF;

namespace Coldairarrow.Api.Controllers.TransferPDF
{
    [NoCheckJWT]
    [Route("/TransferPDF/[controller]/[action]")]
    public class PdfController : BaseApiController
    {
        readonly IHostingEnvironment _hostingEnv;
        readonly IOrder_MasterBusiness _order_MasterBusiness;
        readonly IWarehouse_InformationBusiness _warehouse_InformationBusiness;
        public PdfController(IHostingEnvironment hostingEnv, IOrder_MasterBusiness order_MasterBusiness, IWarehouse_InformationBusiness warehouse_InformationBusiness)
        {
            _hostingEnv = hostingEnv;
            _order_MasterBusiness = order_MasterBusiness;
            _warehouse_InformationBusiness = warehouse_InformationBusiness;
        }
        /// <summary>
        /// 转pdf
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<object> GetPdfsTwo(InputDTO input)
        {

            List<object> dList = new List<object>();

            PdfDocument docHtml = null;

            var Data = string.Empty;
            string Name = "";
            switch (input.Type)
            {
                case 0:
                    var order_Master = _order_MasterBusiness.GetOrderTheDataTree(input);
                    Data = OrderPDF(order_Master);
                    Name = "订单详情.pdf";
                    break;
                case 1:
                    var warehouse_Information1 = _warehouse_InformationBusiness.GetTheData1(input);
                    Data = Warehouse1PDF1(warehouse_Information1);
                    Name = "入库明细.pdf";
                    break;
                case 2:
                    var warehouse_Information2 = _warehouse_InformationBusiness.GetTheData2(input);
                    Data = Warehouse1PDF2(warehouse_Information2);
                    Name = "订单详情.pdf";
                    break;
                case 3:
                    var warehouse_Information3 = _warehouse_InformationBusiness.GetTheData3(input);
                    Data = Warehouse1PDF3(warehouse_Information3);
                    Name = "订单详情.pdf";
                    break;
                case 4:
                    var warehouse_Information4 = _warehouse_InformationBusiness.GetTheData4(input);
                    Data = Warehouse1PDF4(warehouse_Information4);
                    Name = "订单详情.pdf";
                    break;
            }


            HtmlToPdf Renderer = new HtmlToPdf();
            //设置Pdf参数
            Renderer.Options.PdfPageOrientation = PdfPageOrientation.Landscape;//设置页面方式-横向  PdfPageOrientation.Portrait  竖向
            Renderer.Options.PdfPageSize = PdfPageSize.A4;//设置页面大小，30种页面大小可以选择
            Renderer.Options.MarginTop = 10;   //上下左右边距设置  
            Renderer.Options.MarginBottom = 10;
            Renderer.Options.MarginLeft = 10;
            Renderer.Options.MarginRight = 10;
            //设置更多额参数可以去HtmlToPdfOptions里面选择设置

            if (docHtml == null)
                docHtml = Renderer.ConvertHtmlString(Data);//根据html内容导出PDF 
            else
                //在上一个pdf元素页面下面追加Pdf页面，官方文档对于一个pdf文件打印多页的处理提供了分页符，在你想打印一页的元素外面加上   <div style="font-size: 28px; page-break-after: always">元素
                //也就是分页符，但是试用感觉效果并不理想，下面这个Append追加一个pdf页面效果会更好点，但是可能会损耗一些性能
                docHtml.Append(Renderer.ConvertHtmlString(Data));


            string webRootPath = _hostingEnv.ContentRootPath;  //获取项目运行绝对路径
            var path = $"/wwwroot/ExportPDF/{DateTime.Now.ToString("yyyyMMdd")}/";//文件相对路径
            var savepathHtml = $"{webRootPath}{path}{Guid.NewGuid().ToString()}-Html.pdf";//保存绝对路径
            if (!Directory.Exists(Path.GetDirectoryName(webRootPath + path)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(webRootPath + path));
            }
            docHtml.Save(savepathHtml);
            // vs.Pageone = savepathHtml.Replace($"{webRootPath}/wwwroot", " ");

            using (var sw = new FileStream(savepathHtml, FileMode.Open))
            {
                StreamReader mysr = new StreamReader(sw);
                var obj = new object();
                if (!savepathHtml.IsNullOrEmpty())
                {
                    string tPath = "/ExportPDF" + savepathHtml.Split("ExportPDF")[1];
                    obj = new
                    {
                        FileName = Name,
                        FilePath = tPath,
                        UploadTime = DateTime.Now
                    };
                    dList.Add(obj);
                }

            }
            return dList;
        }


        private string OrderPDF(Order_Dto data)
        {
            StringBuilder builder = new StringBuilder();
            #region Table style
            string PayMode;
            switch (data.PayState)
            {
                case 0:
                    PayMode = "未支付";
                    break;
                case 1:
                    PayMode = "支付宝支付";
                    break;
                case 2:
                    PayMode = "微信扫码支付";
                    break;
                default:
                    PayMode = "小程序支付";
                    break;
            }
            #endregion
            #region 2 Table
            string Header = $"<!DOCTYPE html>" +
                $" <html lang='en'>" +
                $" <head> " +
                $" <meta charset='UTF-8'> " +
                $" <meta name='viewport' content='width=device-width, initial-scale=1.0'>" +
                $"   <title>Document</title>" +
                $" </head>" +
                $" <style>   " +
       ".order{width:920px;margin:0 auto;}table{border-collapse:collapse;}.head,.head1{text-align:center;height:40px;}.head td,.head1 td{width:120px;background-color:#eee;}.head td:nth-child(1),.head1 td:nth-child(1){width:300px;}.head1 td:nth-child(3){width:200px;}.item td,.item1 td{text-align:center;height:50px;}.item td:nth-child(1){width:300px;}.item1 td:nth-child(1){width:345px;}" +
$" </style> ";
            string htmlMode =
     $"             <body >" +
   $"   <div class='order'>" +
    $"    <h3>用户信息</h3>" +
   $"             <table border = '1' >" +
   $"          <tr class='head'>" +
   $"             <td>用户昵称</td>" +
   $"             <td>购买人</td>" +
   $"             <td>收货电话</td>" +
   $"             <td>订单总量</td>" +
   $"             <td>订单总价</td>" +
   $"             <td>支付方式</td>" +
   $"         </tr>" +
   $"         <tr class='item'>" +
   $"             <td>" + data.NameUser + "</td>" +
   $"             <td>" + data.payeinfoName + "</td>" +
   $"             <td>" + data.payeinfoPhone + "</td>" +
   $"             <td>" + data.Num + "件</td>" +
   $"             <td>" + data.AllPrice + "元</td>" +
   $"             <td>" + PayMode + "</td>" +
   $"         </tr>" +
   $"     </table>" +

$"    <h3 > 订单信息 </h3 >" +
$"        <table border = '1'>" +
$"             <tr class='head1'>" +
$"                <td>商品名称</td>" +
$"                <td>商品价格</td>" +
$"                <td>商品规格</td>" +
$"                <td>购买单价</td>" +
$"                <td>购买数量</td>" +
$"            </tr>";
            foreach (var item in data._Decimal)
            {
                htmlMode += $"<tr class='item1'><td>" + item.ProductitemNames + "</td><td>" + item.ProductPrice + "元</td><td>" + item.ProductitemNames + "</td><td>" + item.SumProducts + "元</td><td>" + item.Products_Num + "件</td></tr>";
            }
            htmlMode += "</table>" +
$"    </div>" +
$"</body>" +
$"</html>";
            string P = Header + htmlMode;

            #endregion

            // builder.Append(htmlMode);
            //    builder.Append(styHtml);

            return P;
        }
        private string Warehouse1PDF1(Warehouse_InformationDto data)
        {
            StringBuilder builder = new StringBuilder();
            #region Table style
            #endregion
            #region 2 Table
            string Header = $"<!DOCTYPE html>" +
                $" <html lang='en'>" +
                $" <head> " +
                $" <meta charset='UTF-8'> " +
                $" <meta name='viewport' content='width=device-width, initial-scale=1.0'>" +
                $"   <title>Document</title>" +
                $" </head>" +
                $" <style>   " +
       ".order{width:920px;margin:0 auto;}table{border-collapse:collapse;}.head{text-align:center;height:40px;}.head td{width:120px;background-color:#eee;}.head td:nth-child(1){width:300px;}.item td{text-align:center;height:50px;}.item td:nth-child(1){width:300px;}" +
$" </style> ";
            string htmlMode =
     $"             <body >" +
$"    < div class='order'>" +
$"       <h3>出库明细</h3>" +
$"       <table border = '1'>" +
$"           < tr class='head'>" +
$"               <td>商品名称</td>" +
$"               <td>商品规格</td>" +
$"               <td>入库量</td>" +
$"               <td>单价</td>" +
$"               <td>总价</td>" +
$"               <td>上期结余库存</td>" +
$"               <td>本期结余库存</td>" +
$"           </tr>" +
$"           <tr class='item'>" +
$"               <td>"+data.ProductName+"</td>" +
$"               <td>" + data.ProductsName + "</td>" +
$"               <td>"+data.Num +"</td>" +
$"               <td>" + data.Price + "￥</td>" +
$"               <td>" + data.sumPrice + "￥</td>" +
$"               <td>" + data.Info_Oldbalance + "</td>" +
$"               <td>" + data.ThisBlance + "</td>" +
$"           </tr>" +
$"       </table>" +
$"   </div>" +
$"</body>" +
$"</html>";
            string P = Header + htmlMode;

            #endregion

            // builder.Append(htmlMode);
            //    builder.Append(styHtml);

            return P;
        }
        private string Warehouse1PDF2(Warehouse_InformationDto data)
        {
            StringBuilder builder = new StringBuilder();
            #region Table style
            #endregion
            #region 2 Table
            string Header = $"<!DOCTYPE html>" +
                $" <html lang='en'>" +
                $" <head> " +
                $" <meta charset='UTF-8'> " +
                $" <meta name='viewport' content='width=device-width, initial-scale=1.0'>" +
                $"   <title>Document</title>" +
                $" </head>" +
                $" <style>   " +
       ".order{width:920px;margin:0 auto;}table{border-collapse:collapse;}.head{text-align:center;height:40px;}.head td{width:120px;background-color:#eee;}.head td:nth-child(1){width:300px;}.item td{text-align:center;height:50px;}.item td:nth-child(1){width:300px;}" +
$" </style> ";
            string htmlMode =
     $"             <body >" +
$"    < div class='order'>" +
$"       <h3>出库明细</h3>" +
$"       <table border = '1'>" +
$"           < tr class='head'>" +
$"               <td>商品名称</td>" +
$"               <td>商品规格</td>" +
$"               <td>入库次数</td>" +
$"               <td>单价</td>" +
$"               <td>入库量</td>" +
$"               <td>商品库存</td>" +
$"           </tr>" +
$"           <tr class='item'>" +
$"               <td>" + data.ProductName + "</td>" +
$"               <td>" + data.ProductsName + "</td>" +
$"               <td>" + data.DATA0 + "</td>" +
$"               <td>" + data.Price + "￥</td>" +
$"               <td>" + data.Num + "￥</td>" +
$"               <td>" + data.Info_Oldbalance + "</td>" +
$"           </tr>" +
$"       </table>" +
$"   </div>" +
$"</body>" +
$"</html>";
            string P = Header + htmlMode;

            #endregion

            // builder.Append(htmlMode);
            //    builder.Append(styHtml);

            return P;
        }
        private string Warehouse1PDF3(Warehouse_InformationDto data)
        {
            StringBuilder builder = new StringBuilder();
            #region Table style
            #endregion
            #region 2 Table
            string Header = $"<!DOCTYPE html>" +
                $" <html lang='en'>" +
                $" <head> " +
                $" <meta charset='UTF-8'> " +
                $" <meta name='viewport' content='width=device-width, initial-scale=1.0'>" +
                $"   <title>Document</title>" +
                $" </head>" +
                $" <style>   " +
       ".order{width:920px;margin:0 auto;}table{border-collapse:collapse;}.head{text-align:center;height:40px;}.head td{width:120px;background-color:#eee;}.head td:nth-child(1){width:300px;}.item td{text-align:center;height:50px;}.item td:nth-child(1){width:300px;}" +
$" </style> ";
            string htmlMode =
     $"             <body >" +
$"    < div class='order'>" +
$"       <h3>出库明细</h3>" +
$"       <table border = '1'>" +
$"           < tr class='head'>" +
$"               <td>商品名称</td>" +
$"               <td>商品规格</td>" +
$"               <td>入库次数</td>" +
$"               <td>单价</td>" +
$"               <td>入库量</td>" +
$"               <td>商品库存</td>" +
$"           </tr>" +
$"           <tr class='item'>" +
$"               <td>" + data.ProductName + "</td>" +
$"               <td>" + data.ProductsName + "</td>" +
$"               <td>" + data.DATA0 + "</td>" +
$"               <td>" + data.Price + "￥</td>" +
$"               <td>" + data.Num + "￥</td>" +
$"               <td>" + data.Info_Oldbalance + "</td>" +
$"           </tr>" +
$"       </table>" +
$"   </div>" +
$"</body>" +
$"</html>";
            string P = Header + htmlMode;

            #endregion

            // builder.Append(htmlMode);
            //    builder.Append(styHtml);

            return P;
        }
        private string Warehouse1PDF4(Warehouse_InformationDto data)
        {
            StringBuilder builder = new StringBuilder();
            #region Table style
            #endregion
            #region 2 Table
            string Header = $"<!DOCTYPE html>" +
                $" <html lang='en'>" +
                $" <head> " +
                $" <meta charset='UTF-8'> " +
                $" <meta name='viewport' content='width=device-width, initial-scale=1.0'>" +
                $"   <title>Document</title>" +
                $" </head>" +
                $" <style>   " +
       ".order{width:920px;margin:0 auto;}table{border-collapse:collapse;}.head{text-align:center;height:40px;}.head td{width:120px;background-color:#eee;}.head td:nth-child(1){width:300px;}.item td{text-align:center;height:50px;}.item td:nth-child(1){width:300px;}" +
$" </style> ";
            string htmlMode =
     $"             <body >" +
$"    < div class='order'>" +
$"       <h3>出库明细</h3>" +
$"       <table border = '1'>" +
$"           < tr class='head'>" +
$"               <td>商品名称</td>" +
$"               <td>商品规格</td>" +
$"               <td>入库次数</td>" +
$"               <td>单价</td>" +
$"               <td>入库量</td>" +
$"               <td>商品库存</td>" +
$"           </tr>" +
$"           <tr class='item'>" +
$"               <td>" + data.ProductName + "</td>" +
$"               <td>" + data.ProductsName + "</td>" +
$"               <td>" + data.DATA0 + "</td>" +
$"               <td>" + data.Price + "￥</td>" +
$"               <td>" + data.Num + "￥</td>" +
$"               <td>" + data.Info_Oldbalance + "</td>" +
$"           </tr>" +
$"       </table>" +
$"   </div>" +
$"</body>" +
$"</html>";
            string P = Header + htmlMode;

            #endregion

            // builder.Append(htmlMode);
            //    builder.Append(styHtml);

            return P;
        }
    }
}
